package ar.com.e2solver.view.piece;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Panel;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Dimension2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.List;

import ar.com.e2solver.conf.PuzzleTheme;
import ar.com.e2solver.model.Piece;

public class PieceShapeView extends Panel {

	private static final long serialVersionUID = 1L;
	
	private Piece piece;
	
	public PieceShapeView(Piece piece){
		this.piece = piece;
		setPreferredSize(new Dimension(32,32));
		setSize(new Dimension(32,32));
		setForeground(Color.WHITE);
		setBackground(Color.DARK_GRAY);
	}
	
	@Override
	public void paint(Graphics graphics) {
		Dimension2D size = getSize();
		double wd = size.getWidth();
		double ht = size.getHeight();
		double minsize = Math.min(wd, ht);
		Graphics2D g2d = (Graphics2D) graphics;

		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

		double dx = wd / 2;
		double dy = ht / 2;

		Shape prev_clip = g2d.getClip();
		g2d.clip(new Rectangle2D.Double(0, 0, wd - 0.5, ht - 0.5));
		AffineTransform prev_trx = g2d.getTransform();

		List<Pattern> patterns = Pattern.getAllPatterns();
		g2d.translate(0, -dy);
		patterns.get(PuzzleTheme.getNumberForColor(piece.getTop())).paint(g2d, size);
		g2d.translate(dx, dy);
		patterns.get(PuzzleTheme.getNumberForColor(piece.getRight())).paint(g2d, size);
		g2d.translate(-dx, dy);
		patterns.get(PuzzleTheme.getNumberForColor(piece.getBottom())).paint(g2d, size);
		g2d.translate(-dx, -dy);
		patterns.get(PuzzleTheme.getNumberForColor(piece.getLeft())).paint(g2d, size);

		g2d.setTransform(prev_trx);
		g2d.setClip(prev_clip);

		g2d.setStroke(new BasicStroke((float) (minsize / 50.)));
		g2d.draw(new Rectangle2D.Double(0, 0, wd, ht));

		g2d.draw(new Line2D.Double(0, 0, wd, ht));
		g2d.draw(new Line2D.Double(wd, 0, 0, ht));

	}

}
